//
//  JXReplyMineCtrl.m
//  JiaoX
//
//  Created by yinqy on 15/2/27.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//  回复我的

#import "JXReplyMineCtl.h"
#import "JXReplyMineChildCtl.h"
#define TITLE_BTN_HEIGHT 45

@interface JXReplyMineCtl ()<UIScrollViewDelegate>
{
    UIButton *replyComtBtn;
    UIButton *replyPostBtn;
    
    UIImageView *_accessView;
    
    UIButton *_currentBtn;
}
@property(nonatomic,strong)NSMutableArray *childController;
@property(nonatomic,weak)UIScrollView *scrollView;
@end
@implementation JXReplyMineCtl

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"回复我的"];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    _childController = [NSMutableArray array];
    [self initView];
}

-(void)initView{
    _accessView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_order_access"]];
    
    
    replyComtBtn = [self getTitleBtnWithTitle:@"回复评论" point:CGPointMake(0, NavHeight)];
    [replyComtBtn setTag:100];
    [self titleBtnClick:replyComtBtn];
    replyPostBtn = [self getTitleBtnWithTitle:@"回复帖子" point:CGPointMake(JXScreenW/2,NavHeight)];
    UIView *red = [replyPostBtn viewWithTag:55];
    [red setHidden:[UserDefaultsHelper integerForKey:KEY_REPLY_MINE_POST_NOTI_NUM]==0];
    [replyPostBtn setTag:101];
    
    [self.view addSubview:replyComtBtn];
    [self.view addSubview:replyPostBtn];
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(replyPostBtn.frame), JXScreenW, self.view.height-NavHeight-TITLE_BTN_HEIGHT)];
    _scrollView = scrollView;
    [self.view addSubview:scrollView];
    
    for (int i=0; i<2; i++) {
        JXReplyMineChildCtl *ctr = [JXReplyMineChildCtl getInstanceWithReplyType:[NSString stringWithFormat:@"%d",i==0?1:0]];
        [ctr.view setFrame:CGRectMake(i*JXScreenW, 0, JXScreenW, _scrollView.height)];
        [_childController addObject:ctr];
        [_scrollView addSubview:ctr.view];
    }
    [_scrollView setContentSize:CGSizeMake(JXScreenW*_childController.count, _scrollView.height)];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
}

-(UIButton *)getTitleBtnWithTitle:(NSString *)title point:(CGPoint)point{
    UIButton *btn = [[UIButton alloc]init];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor whiteColor]];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:15]]];
    [btn setTitleColor:JXThemeColor forState:UIControlStateSelected];
    [btn setTitleColor:JXColor(77, 77, 77) forState:UIControlStateNormal];
    [btn setFrame:CGRectMake(point.x, point.y, JXScreenW/2, TITLE_BTN_HEIGHT)];
    [btn addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *red = [[UIView alloc]init];
    [red setSize:CGSizeMake(5, 5)];
    [red setBackgroundColor:[UIColor redColor]];
    [red.layer setCornerRadius:red.width/2];
    [red.layer setMasksToBounds:YES];
    [red setY:10];
    [red setX:btn.width-40];
    [red setTag:55];
    [red setHidden:YES];
    [btn addSubview:red];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, TITLE_BTN_HEIGHT-0.5, JXScreenW/2, 0.5)];
    [view setBackgroundColor:JXColor(204, 204, 204)];
    
    [btn addSubview:view];
    return btn;
}


-(void)titleBtnClick:(UIButton *)btn{
    [[btn viewWithTag:55] setHidden:YES];
    if (btn.tag==100) {
        [UserDefaultsHelper setInteger:0 forKey:KEY_REPLY_MINE_COMT_NOTI_NUM];
    }else{
        [UserDefaultsHelper setInteger:0 forKey:KEY_REPLY_MINE_POST_NOTI_NUM];
    }
    if(_currentBtn.tag ==btn.tag){
        return;
    }else{
        btn.selected = YES;
        _currentBtn.selected = false;
        _currentBtn = btn;
        [self setScrollCurrentPageNum:(int)btn.tag-100];
        
        [_accessView removeFromSuperview];
        _accessView.y = TITLE_BTN_HEIGHT-_accessView.height;
        _accessView.x = (JXScreenW/2-_accessView.width)/2;
        [btn addSubview:_accessView];
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //当前页数
    int pageNum = (scrollView.contentOffset.x+scrollView.width*0.5)/scrollView.width;
    [self setScrollCurrentPageNum:pageNum];
    UIButton *btn = (UIButton *)[self.view viewWithTag:pageNum+100];
    [self titleBtnClick:btn];
}

-(void)setScrollCurrentPageNum:(int)pageNum{
    CGPoint p = CGPointMake(pageNum*_scrollView.frame.size.width, 0);
    [_scrollView setContentOffset:p];
}

-(BOOL)shouldLogin{
    return YES;
}

@end
